from pydantic import BaseModel, Field, validator
from typing import List, Dict

class Data(BaseModel):
    year: int = Field(description="Year")
    month: int = Field(description="Month")
    day: int = Field(description="Day")
    era: str = Field(description="BC or AD")

    @validator('month')
    def valid_month(cls, value):
        if value <= 0 or value > 12:
            raise ValueError("月份必须在1-12月之间")
        return value

    @validator('day')
    def valid_day(cls, value):
        if value <= 0 or value > 31:
            raise ValueError("日期必须在1-31之间")
        return value

    @validator('day', pre=True, always=True)
    def valid_data(cls, value, values):
        year = values.get('year')
        month = values.get('month')
        if year is None or month is None:
            return value
        if month == 2:
            if cls.is_leap_year(year) and value > 29:
                raise ValueError("闰年2月只有29天")
            elif not cls.is_leap_year(year) and value > 28:
                raise ValueError("闰年2月只有28天")
        elif month in [4, 6, 9, 11] and value > 30:
            raise ValueError(f"{month}月只有30天")
        return value

    @staticmethod
    def is_leap_year(year):
        if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
            return True
        return False
